package com.twu.biblioteca;

import java.io.IOException;

public class BibliotecaApp {

    Console console;

    public BibliotecaApp() {

        this.console = new Console();
    }

    private void displayWelcomeMessage() {
        console.print("*****************************");
        console.print("*** Welcome To Biblioteca ***");
        console.print("*****************************");
    }


    private void displayMainMenu() {
        console.print("\n---MENU---\n");
        console.print("<1> List Books");
        console.print("<2> Checkout Book");
        console.print("<3> Return Book");
        console.print("<4> List Movies");
        console.print("<5> Checkout Movie");
        console.print("<6> View My Account Information");
        console.print("<7> Exit Application");
        console.print("Enter Choice : ");
    }

    private void displayBookListHeader() {
        console.print("------------------------------------------------------------");
        console.print(console.formatString("NAME AUTHOR YEAR_PUBLISHED"));
        console.print("------------------------------------------------------------");
    }

    private void displayMovieListHeader() {
        console.print("--------------------------------------------------------------------------------");
        console.print(console.formatString("NAME DIRECTOR YEAR_RELEASED RATING"));
        console.print("--------------------------------------------------------------------------------");
    }


    private void mainMenu() throws IOException {

        Library library = Library.getLibraryInstance();

        LibraryCommand listBooks = new ListBooks(library);
        LibraryCommand checkoutBook = new CheckoutBook(library);
        LibraryCommand returnBook = new ReturnBook(library);
        LibraryCommand listMovies = new ListMovies(library);
        LibraryCommand checkoutMovie = new CheckoutMovie(library);
        LibraryCommand userInfo = new UserInfo(library);

        UserOperation userOperation = new UserOperation();

        while(true) {

            displayMainMenu();
            switch (console.getIntergerInputFromUser()) {
                case 1:
                    displayBookListHeader();
                    userOperation.executeUserOperation(listBooks);
                    break;

                case 2:
                    userOperation.executeUserOperation(checkoutBook);
                    break;

                case 3:
                    userOperation.executeUserOperation(returnBook);
                    break;

                case 4:
                    displayMovieListHeader();
                    userOperation.executeUserOperation(listMovies);
                    break;

                case 5:
                    userOperation.executeUserOperation(checkoutMovie);
                    break;

                case 6:
                    userOperation.executeUserOperation(userInfo);
                    break;

                case 7:
                    return;

                default:
                    console.print("Select a valid option !");
            }
        }
    }

    private void displayMenu() throws IOException {
        displayWelcomeMessage();
        mainMenu();
    }

    public static void main(String[] args) throws IOException {

        Console console = new Console();
        Library library = Library.getLibraryInstance();
        Database DB = new Database(library);
        BibliotecaApp App = new BibliotecaApp();

        library.authenticateUserLogin();

        App.displayMenu();

        console.print("Thank you for using Biblioteca...");
        System.exit(0);
    }

}
